<!DOCTYPE html>
<html>
    <head>
        <meta name="layout" content="main" />
        <g:set var="entityName" value="${message(code: 'assess.label', default: 'Assess')}" />
        <title>房产评估</title>
    </head>
    <style>

    </style>
    <body>

    <div class="small-header">
        <div class="hpanel">
            <div class="panel-body">
                <div id="hbreadcrumb" class="pull-right navFloatRight">
                    <ol class="hbreadcrumb breadcrumb">
                        <li>CINDATA PLS</li>
                        <li class="">
                            <span>评估</span>
                        </li>
                        <li class="active">
                            <span>房产评估</span>
                        </li>
                    </ol>
                </div>

                <h2 class="font-light m-b-xs">
                    房产评估
                </h2>
            </div>
        </div>
    </div>

    <div class="content animate-panel container">
        %{--房产评估--}%
        <div class="row">
            <div class="hpanel">
                <div class="col-md-12">
                    <div class="hpanel hgreen">
                        <div class="panel-heading">
                            <div class="panel-tools">
                                <a class="showhide"><i class="fa fa-chevron-up"></i></a>
                            </div>
                            房产评估
                        </div>

                        <div class="panel-body">
                            <div class="row form-group">
                                <div class="col-sm-6 col-sm-offset-3">
                                    <h5 style="margin-bottom: 12px;">城市</h5>
                                    <g:select class="form-control" name="city" id="city" from="${com.cindata.City.executeQuery("select distinct e.city as city from Estate e ")}" optionKey="name" optionValue="name" value="${params?.city}" noSelection="${["": '请选择城市']}"/>
                                    <input type="text" class="hide" id="communityId" value="${params?.communityId}" name="communityId" />
                                    <input type="text" class="hide" id="buildingId" value="${params?.buildingId}" name="buildingId" />
                                    <input type="text" class="hide" id="unitId" value="${params?.unitId}" name="unitId" />
                                    <input type="text" class="hide" id="houseId" value="${params?.houseId}" name="houseId" />
                                </div>
                            </div>
                            <div class="row form-group">
                                <div class="col-sm-6 col-sm-offset-3">
                                    <h5 style="margin-bottom: 12px;">房产地址<span style="color: #ff0000">*</span></h5>

                                    <div class="input-group">
                                        <input type="text" id="address" name="address" class="form-control" placeholder="请输入房产地址">
                                        <span class="input-group-btn">
                                             <button type="button" id="splitMatch" class="btn btn-primary">查询匹配</button>
                                        </span>
                                    </div>
                                </div>
                            </div>

                            <div class="row form-group">
                                <div class="col-sm-6 col-sm-offset-3">
                                    <h5 style="margin-bottom: 12px;">小区名称</h5>
                                    <input type="text" id="communityName" name="communityName" class="form-control" readonly="true" style="background: #f6f6f6;" />
                                </div>
                            </div>

                            <div class="row form-group">
                                <div class="col-sm-2  col-sm-offset-3">
                                    <h5 style="margin-bottom: 12px;">楼号</h5>
                                    <select class="form-control buildingName" name="buildingTemp" id="buildingTemp" style="position: relative"></select>
                                    <input id="building" name="building" class="form-control" style="position: absolute;height: 26px;top: 37px;left: 17px;width: 66%;border: none;box-shadow: none;">
                                </div>

                                <div class="col-sm-2">
                                    <h5 style="margin-bottom: 12px;">单元号</h5>
                                    <select class="form-control unitName" name="unitTemp" id="unitTemp" style="position: relative"></select>
                                    <input id="unit" name="unit" class="form-control " style="position: absolute;height: 26px;top: 37px;left: 17px;width: 66%;border: none;box-shadow: none;">
                                </div>

                                <div class="col-sm-2">
                                    <h5 style="margin-bottom: 12px;">房号</h5>
                                    <select class="form-control houseName" name="houseTemp" id="houseTemp" style="position: relative"></select>
                                    <input id="house" name="house" class="form-control " placeholder="" style="position: absolute;height: 26px;top: 37px;left: 17px;width: 66%;border: none;box-shadow: none;">
                                </div>
                            </div>

                            <div class="row form-group">
                                <div class="col-sm-2 col-sm-offset-3">
                                    <h5 style="margin-bottom: 12px;">所在层数<span style="color: #ff0000">*</span></h5>
                                    <input id="floor" name="floor" class="form-control">
                                </div>

                                <div class="col-sm-2">
                                    <h5 style="margin-bottom: 12px;">地上总层数<span style="color: #ff0000">*</span></h5>
                                    <input id="totalFloor" name="totalFloor" class="form-control">
                                </div>

                                <div class="col-sm-2">
                                    <h5 style="margin-bottom: 12px;">建成年份</h5>
                                    <input id="buildYear" name="buildYear" class="form-control">
                                </div>
                            </div>

                            <div class="row form-group">
                                <div class="col-sm-2 col-sm-offset-3">
                                    <h5 style="margin-bottom: 12px;">面积<span style="color: #ff0000">*</span></h5>
                                    <div class="input-group m-b"><input type="text" id="area" name="area" class="form-control"><span class="input-group-addon">平方米</span></div>
                                </div>

                                <div class="col-sm-2">
                                    <h5 style="margin-bottom: 12px;">成交总价</h5>
                                    <div class="input-group m-b"><input type="text" id="finalPrice" name="finalPrice" class="form-control"><span class="input-group-addon">元</span></div>
                                </div>

                                <div class="col-sm-2">
                                    <h5 style="margin-bottom: 12px;">外部评估总价</h5>
                                    <div class="input-group m-b"><input type="text" id="externalAssessPrice" name="externalAssessPrice" class="form-control"><span class="input-group-addon">元</span></div>
                                </div>
                            </div>

                            <div class="row form-group text-center">
                                <button class="ladda-button btn btn-primary quick" data-style="zoom-in" style="margin-right: 15px;"><span class="ladda-label">快速评估</span><span class="ladda-spinner"></span></button>
                                <button class="ladda-button btn btn-primary artificial" data-style="zoom-in" style="margin-right: 15px;"><span class="ladda-label">人工评估</span><span class="ladda-spinner"></span></button>
                                <button class="btn" type="button" id="resetBtn" style="width: 82px;">重置</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>

        $(function () {

            // 地址拆分匹配
            $("#splitMatch").click(function() {

                reset(false);

                var city = $("#city").val();
                if (!city) 
                {
                    toastr.error('请输入城市！');
                    return
                }
                var address = $("#address").val();
                if (!address) 
                {
                    toastr.error('请输入房产地址！');
                    return
                }

                $.ajax({
                    url:'/assess/splitMatch',
                    data:{
                        'address':address,
                        'city' : city
                    },
                    success: function (data) {
                        if (data.status == "success") 
                        {
                            var result = data.addressSplitList
                            var communityId = result.match_community_id
                            if (communityId && communityId != "-1")
                            {
                                $("#buildYear").val(data.buildYear);
                                $("#communityId").val(result.match_community_id);
                                $("#communityName").val(result.match_community_name);
                                $("#building").val(result.match_building_name);
                                $("#unit").val(result.match_unit_name);
                                $("#house").val(result.match_house_name);

                                // 根据小区带出楼栋
                                $.ajax({
                                    type: "POST",
                                    url: "/assess/getBuilding",
                                    data: {
                                        city: city,
                                        communityId: communityId
                                    },
                                    success: function (data) {
                                        if (data.status == "success") 
                                        {
                                            var communityList = data.communityList;
                                            var option;
                                            if (data.status == "success" && communityList != null && communityList != '') {
                                                $('.buildingName').html("");
                                                $(".buildingName").append('<option value="">请选择</option>')
                                                for (var i = 0; i < communityList.length; i++) {
                                                    option = '<option value="' + communityList[i].buildingId + '">' + communityList[i].buildingName + '</option>';
                                                    $(".buildingName").append(option)
                                                }
                                                $("select").select2();
                                            }
                                        }
                                    }
                                });
                            }
                            else
                            {
                                toastr.error('对不起，目前不能对此地址匹配！建议您输入必输项后点击按钮进行人工评估。');
                            }
                        }
                        else
                        {
                            swal("解析失败，请稍后重试", data.errorMessage, "error");
                        }
                    },
                    error: function () {
                        swal("解析失败，请稍后重试", "", "error");
                    }
                })
            });

            var l1 = $( '.quick' ).ladda();
            l1.click(function(){
                l1.ladda( 'start' );
                isFrequencyContinue("1")
            });

            var l2 = $( '.artificial' ).ladda();
            l2.click(function(){
                l2.ladda( 'start' );
                isFrequencyContinue("2");
            });

            function timeOut(em)
            {
                if (em == "1") {
                    setTimeout(function(){
                        l1.ladda('stop');
                    },3000)
                }
                if (em == "2") {
                    setTimeout(function(){
                        l2.ladda('stop');
                    },3000)
                }
            }

            // // 快速评估
            // $("#quick").click(function() {
            //     isFrequencyContinue("1")
            // });

            // 人工评估
            // $("#artificial").click(function() {
            //     isFrequencyContinue("2")
            // })

            function isFrequencyContinue(em)
            {
                var address = $("#address").val();
                if (!address) 
                {
                    timeOut(em);
                    toastr.error('请输入房产地址！');
                    return
                }

                var flag = ifFrequency(address)
                if (flag)
                {
                    swal({
                        title: "此地址被标记高评倾向",
                        text: "是否继续评估？",
                        type: "warning",
                        showCancelButton: true,
                        confirmButtonColor: "#DD6B55",
                        confirmButtonText: "继续评估",
                        cancelButtonText: "不，取消评估",
                        closeOnConfirm: false,
                        closeOnCancel: false },
                    function (isConfirm) {
                        if (isConfirm)
                        {
                            swal("继续评估", "继续评估", "success");
                            assessFunction(em, null);
                        } 
                        else 
                        {
                            timeOut(em);
                            swal("取消评估", "取消评估成功", "success");
                            return
                        }
                    });
                }
                else
                {
                    assessFunction(em, null);
                }
            }

            function assessFunction(em, assessId)
            {
                var communityId = $("#communityId").val();
                var city = $("#city").val();
                if (!city) 
                {
                    timeOut(em);
                    toastr.error('请输入城市！');
                    return
                }
                var address = $("#address").val();
                if (!address) 
                {
                    timeOut(em);
                    toastr.error('请输入房产地址！');
                    return
                }
                var communityName = $("#communityName").val();
                if (em == "1" && !communityName)
                {
                    timeOut(em);
                    toastr.error('请输入小区名称！');
                    return
                }
                var building = $("#building").val();
                var unit = $("#unit").val();
                var house = $("#house").val();
                var floor = $("#floor").val();
                if (!floor) 
                {
                    timeOut(em);
                    toastr.error('请输入所在层数！');
                    return
                }
                if (!(/^-?[1-9]\d*$/.test(floor))) 
                {
                    timeOut(em);
                    toastr.error("请输入正确的所在楼层！");
                    return;
                }
                var totalFloor = $("#totalFloor").val();
                if (!totalFloor) 
                {
                    timeOut(em);
                    toastr.error('请输入地上总层数！');
                    return
                }
                if (!(/^-?[1-9]\d*$/.test(totalFloor)) || totalFloor <= 0) 
                {
                    timeOut(em);
                    toastr.error("请输入正确的地上总层数！");
                    return;
                }
                if (parseInt(floor) > parseInt(totalFloor)) 
                {
                    timeOut(em);
                    toastr.error("所在楼层必须小于等于地上总楼层");
                    return;
                }
                var buildYear = $("#buildYear").val();
                if (buildYear && !(/^-?[1-9]\d*$/.test(buildYear))) 
                {
                    timeOut(em);
                    toastr.error("请输入正确的建成年份！");
                    return;
                }
                if (buildYear && buildYear <= 0) 
                {
                    timeOut(em);
                    toastr.error("请输入正确的建成年份！");
                    return;
                }
                var area = $("#area").val();
                if (!area) 
                {
                    timeOut(em);
                    toastr.error('请输入面积！');
                    return
                }
                if (!(/^-?[1-9]\d*$/.test(area))) 
                {
                    timeOut(em);
                    toastr.error("请输入正确的面积！");
                    return;
                }
                if (area <= 0) 
                {
                    timeOut(em);
                    toastr.error('面积必须大于0！');
                    return;
                }
                var finalPrice = $("#finalPrice").val();
                if (!finalPrice) 
                {
                    finalPrice = 0
                }
                if (finalPrice && !(/^-?[1-9]\d*$/.test(finalPrice))) 
                {
                    timeOut(em);
                    toastr.error("请输入正确的成交总价！");
                    return;
                }
                if (finalPrice < 0) 
                {
                    timeOut(em);
                    toastr.error('成交总价必须大于0！');
                    return;
                }
                var externalAssessPrice = $("#externalAssessPrice").val();
                if (!externalAssessPrice) 
                {
                    timeOut(em);
                    externalAssessPrice = 0
                }
                if (externalAssessPrice && !(/^-?[1-9]\d*$/.test(externalAssessPrice))) 
                {
                    timeOut(em);
                    toastr.error("请输入正确的外部评估总价！");
                    return;
                }
                if (externalAssessPrice < 0) 
                {
                    timeOut(em);
                    toastr.error('外部评估总价必须大于0！');
                    return;
                }

                $.ajax({
                    url:'/assess/propertyAssess',
                    data:{
                        'assessId': assessId,
                        'em': em,
                        'communityId': communityId,
                        'city' : city,
                        'address':address,
                        'communityName': communityName,
                        'building': building,
                        'unit': unit,
                        'house': house,
                        'floor': floor,
                        'totalFloor': totalFloor,
                        'buildYear': buildYear,
                        'area': area,
                        'finalPrice': finalPrice,
                        'externalAssessPrice': externalAssessPrice
                    },
                    success:function(data)
                    {
                        // 快速评估
                        if (em == "1")
                        {
                            var status = data.status
                            var result = data.result
                            var assessId
                            var assess = data.assess
                            if (assess)
                            {
                                assessId = assess.id
                            }
                            if (status = "success" && result.code == "0")
                            {
                                window.location.href = "/assess/show/" + assessId;
                            }
                            else
                            {
                                swal({
                                    title: "此地址暂不能评估",
                                    text: "是否转为人工评估？",
                                    type: "warning",
                                    showCancelButton: true,
                                    confirmButtonColor: "#DD6B55",
                                    confirmButtonText: "转为人工评估",
                                    cancelButtonText: "不，取消评估",
                                    closeOnConfirm: false,
                                    closeOnCancel: false },
                                function (isConfirm) {
                                    if (isConfirm) {
                                        assessFunction("2", assessId);
                                        swal("已转为人工评估！", "请在30分钟后查看评估记录。", "success");
                                    } else {
                                        timeOut(em);
                                        swal("取消评估", "取消评估成功", "success");
                                    }
                                });
                            }
                        }

                        // 人工评估
                        if (em == "2") 
                        {
                            var status = data.status
                            var result = data.result
                            if (status = "success" && result.code == "4")
                            {
                                swal({
                                    title: "人工评估已提交",
                                    text: "请在30分钟后查看评估记录。",
                                    type: "success",
                                    confirmButtonText: "OK",
                                    closeOnConfirm: false,
                                    closeOnCancel: false },
                                function (isConfirm) {
                                    if (isConfirm) {
                                        window.location.href = "/assess/history";
                                    }
                                });
                            }
                            else
                            {
                                timeOut(em);
                                swal("人工评估失败", "评估失败，请稍后重试", "error");
                            }
                        }
                    }
                })
            }

            // 检查是否高评倾向
            function ifFrequency(address)
            {
                var flag = false
                $.ajax({
                    type: "POST",
                    url: "/assess/ifFrequency",
                    data: {
                        address: address
                    },
                    async: false,
                    success: function (data) {
                        if (data.status == "success")
                        {
                            flag = data.flag
                        }
                    },
                    error: function () {
                        swal("检查是否高评倾向失败，请稍后重试", "", "error");
                    }
                });

                return flag;
            }

            $("#buildingTemp").change(function() {
                $("#building").val($(this).find("option:selected").text());
                $("#buildingId").val($(this).find("option:selected").val());

                var buildingId = $(this).find("option:selected").val()
                var communityId = $("#communityId").val();
                var city = $("#city").val();

                // 根据楼栋带出单元
                $.ajax({
                    type: "POST",
                    url: "/assess/getUnit",
                    data: {
                        city: city,
                        communityId: communityId,
                        buildingId: buildingId,
                    },
                    success: function (data) {
                        if (data.status == "success") {
                            var unitList = data.unitList;
                            var option;
                            if (data.status == "success" && unitList != null && unitList != '') {
                                $('.unitName').html("");
                                $(".unitName").append('<option value="">请选择</option>')
                                for (var i = 0; i < unitList.length; i++) {
                                    option = '<option value="' + unitList[i].unitId + '">' + unitList[i].unitName + '</option>';
                                    $(".unitName").append(option)
                                }
                                $("select").select2();
                            }
                        }
                    },
                    error: function () {
                        swal("获取失败，请稍后重试", "", "error");
                    }
                });

            })

            $("#unitTemp").change(function() {

                $("#unit").val($(this).find("option:selected").text());
                $("#unitId").val($(this).find("option:selected").val());


                var unitId = $(this).find("option:selected").val()
                var buildingId = $("#buildingId").val();
                var city = $("#city").val();

                // 根据单元带出户号
                $.ajax({
                    type: "POST",
                    url: "/assess/getHouse",
                    data: {
                        city: city,
                        buildingId: buildingId,
                        unitId: unitId,
                    },
                    success: function (data) {
                        if (data.status == "success") {
                            var houseList = data.houseList;
                            var option;
                            if (data.status == "success" && houseList != null && houseList != '') {

                                $('.houseName').html("");
                                $(".houseName").append('<option value="">请选择</option>')
                                for (var i = 0; i < houseList.length; i++) {
                                    option = '<option value="' + houseList[i].houseId + '" floor="' + houseList[i].floor + '" totalFloor="' + houseList[i].totalfloor + '">' + houseList[i].house + '</option>';
                                    $(".houseName").append(option)
                                }
                                $("select").select2();
                            }
                        }
                    },
                    error: function () {
                        swal("获取失败，请稍后重试", "", "error");
                    }
                });
            })

            $("#houseTemp").change(function() {

                $("#house").val($(this).find("option:selected").text());
                $("#houseId").val($(this).find("option:selected").val());

                var floor = $(this).find("option:selected").attr("floor");
                if (floor && floor != "null") 
                {
                    $("#floor").val(floor);
                }

                var totalFloor = $(this).find("option:selected").attr("totalFloor");
                if (totalFloor && totalFloor != "null")
                {
                    $("#totalFloor").val(totalFloor);
                }
            })

            $("#resetBtn").click(function() {
                reset(true);
            })

            function reset(reset)
            {
                if (reset)
                {
                    $("#address").val("");

                    $("#city").val("广州市");
                    $("#s2id_city span:first").html("广州市");
                }

                $("#communityId").val("");
                $("#communityName").val("");

                $("#buildingId").val("");
                $("#building").val("");
                $("#buildingTemp").val("");
                $("#s2id_buildingTemp span:first").html("");
                $('.buildingName').html("");

                $("#unitId").val("");
                $("#unit").val("");
                $("#unitTemp").val("");
                $("#s2id_unitTemp span:first").html("");
                $('.unitName').html("");

                $("#houseId").val("");
                $("#house").val("");
                $("#houseTemp").val("");
                $("#s2id_houseTemp span:first").html("");
                $('.houseName').html("");

                $("#floor").val("");
                $("#totalFloor").val("");
                $("#buildYear").val("");
                $("#area").val("");
                $("#finalPrice").val("");
                $("#externalAssessPrice").val("");

                $("select").select2();
            }
        });

    </script>

    </body>
</html>
